package com.heima.homeservicemanagementsystem.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.homeservicemanagementsystem.entity.Employees;
import com.heima.homeservicemanagementsystem.service.EmployeesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.heima.homeservicemanagementsystem.module.PageParams;
import com.heima.homeservicemanagementsystem.module.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.heima.homeservicemanagementsystem.entity.AccountSettings;
import com.heima.homeservicemanagementsystem.service.AccountSettingsService;
import com.heima.homeservicemanagementsystem.mapper.AccountSettingsMapper;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import static cn.dev33.satoken.SaManager.log;

/**
 * 账户设置;(account_settings)表控制层
 *
 * @author : http://www.chiner.pro
 * @date : 2025-4-23
 */
@Slf4j
@Tag(name = "账户设置对象功能接口")
@RestController
@RequestMapping("/accountSettings")
public class AccountSettingsController {

    @Resource
    private AccountSettingsService accountSettingsService;

    @Resource
    private AccountSettingsMapper accountSettingsMapper;
    @Resource
    private EmployeesService employeesService;

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public Result<Page<AccountSettings>> paginQuery(@RequestBody PageParams<AccountSettings> pageParams) {
        //查询参数
        AccountSettings params = pageParams.getParams();
        LambdaQueryWrapper<AccountSettings> lqw = new LambdaQueryWrapper<>();


        lqw.eq(params.getUserId() != null, AccountSettings::getUserId, params.getUserId());


        lqw.eq(params.getAccountName() != null, AccountSettings::getAccountName, params.getAccountName());


        lqw.eq(params.getBank() != null, AccountSettings::getBank, params.getBank());


        lqw.eq(params.getOpeningBank() != null, AccountSettings::getOpeningBank, params.getOpeningBank());


        lqw.eq(params.getOutlets() != null, AccountSettings::getOutlets, params.getOutlets());


        lqw.eq(params.getBankAccountNumber() != null, AccountSettings::getBankAccountNumber, params.getBankAccountNumber());


        lqw.eq(params.getAccountOpeningCertificate() != null, AccountSettings::getAccountOpeningCertificate, params.getAccountOpeningCertificate());


        lqw.eq(params.getExamine() != null, AccountSettings::getExamine, params.getExamine());


        return Result.success(accountSettingsService.page(pageParams.getPage(), lqw));
    }


    @Operation(summary = "账户设置所有搜索")
    @GetMapping("/list")
    public Result searchQuery() {
        List<AccountSettings> list = accountSettingsService.list();
        for (AccountSettings accountSettings : list) {
            Employees one = employeesService.getOne(Wrappers.<Employees>lambdaQuery().eq(Employees::getId, accountSettings.getUserId()));
            one.setPassword("");
            one.setSlat("");
            accountSettings.setEmployees(one);
        }


        return Result.success(list);
    }

    @Operation(summary = "有条件的搜索")
    @PostMapping("/tjSearch")
    public Result Query(@RequestBody AccountSettings accountSettings) {
        log.info("条件搜索信息：{}", accountSettings);
        LambdaQueryWrapper<AccountSettings> lqw = new LambdaQueryWrapper<>();


        lqw.eq(accountSettings.getId() != null, AccountSettings::getId, accountSettings.getId());





        lqw.like(accountSettings.getAccountName() != null, AccountSettings::getAccountName, accountSettings.getAccountName());


        lqw.like(accountSettings.getBank() != null, AccountSettings::getBank, accountSettings.getBank());





        lqw.eq(accountSettings.getExamine() != null, AccountSettings::getExamine, accountSettings.getExamine());


        lqw.eq(accountSettings.getPass() != null, AccountSettings::getPass, accountSettings.getPass());
        List<AccountSettings> list = accountSettingsService.list(lqw);
        for (AccountSettings account : list) {
            Employees one = employeesService.getOne(Wrappers.<Employees>lambdaQuery().eq(Employees::getId, account.getUserId()));
            one.setPassword("");
            one.setSlat("");
            account.setEmployees(one);
        }
        return Result.success(list);
    }

    /**
     * 新增数据
     *
     * @param accountSettings 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增数据")
    @PostMapping("/insert")
    public Result insert(@RequestBody AccountSettings accountSettings) {
        log.info("accountSettings:" + accountSettings);
//        AccountSettings one = accountSettingsService.getOne(Wrappers.<AccountSettings>lambdaQuery().eq(AccountSettings::getUserId, accountSettings.getUserId()));
        boolean exists = accountSettingsService.exists(Wrappers.<AccountSettings>lambdaQuery().eq(AccountSettings::getUserId, accountSettings.getUserId()));
        if (exists) {
            accountSettings.setExamine("false");
            accountSettings.setPass("false");
            accountSettings.setSuggestedModifications("");
            accountSettingsService.updateById(accountSettings);
            return Result.success("该用户已申请");
        }
        accountSettings.setExamine("false");
        accountSettings.setPass("false");
        accountSettings.setSuggestedModifications("");
        if (accountSettingsService.save(accountSettings)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 更新数据
     *
     * @param accountSettings 实例对象
     * @return 实例对象
     */

    @Operation(summary = "更新数据")
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody AccountSettings accountSettings) {
        if (accountSettingsService.updateById(accountSettings)) {
            return Result.success();
        }
        return Result.error();
    }

    @Operation(summary = "通过用户名字查询")
    @PostMapping("/queryByid")
    public Result queryByid(@RequestBody Map<Object, String> userId) {
        log.info("userId:" + userId);
        String userId1 = userId.get("userId");
        AccountSettings one = accountSettingsService.getOne(Wrappers.<AccountSettings>lambdaQuery().eq(AccountSettings::getUserId, userId1));
        if (Objects.nonNull(one)) {
            return Result.success(one);
        }

        return Result.error();
    }

    @Operation(summary = "后台的反馈信息")

    @PutMapping("/htupdate")
    public Result<Boolean> htupdate(@RequestBody AccountSettings accountSettings) {
        log.info("信息" + accountSettings);
        LambdaUpdateWrapper<AccountSettings> objectUpdateWrapper = new LambdaUpdateWrapper<>();

        objectUpdateWrapper.set(StringUtils.hasLength(accountSettings.getSuggestedModifications()), AccountSettings::getSuggestedModifications, accountSettings.getSuggestedModifications());
        objectUpdateWrapper.set(StringUtils.hasLength(accountSettings.getExamine()), AccountSettings::getExamine, accountSettings.getExamine());
        objectUpdateWrapper.set(StringUtils.hasLength(accountSettings.getPass()), AccountSettings::getPass, accountSettings.getPass());

        objectUpdateWrapper.eq(AccountSettings::getId, accountSettings.getId());

        if (accountSettingsMapper.update(null, objectUpdateWrapper) > 0) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据", parameters = {@Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)})
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteById(@PathVariable String id) {

        if (accountSettingsService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }

    @Operation(summary = "多id删除数据")
    @DeleteMapping("/delete")
    public Result<Boolean> deleteByIds(@RequestBody List<String> id) {
        log.info("前端信息：{}", id);
        if (accountSettingsService.removeByIds(id)) {
            return Result.success();
        }
        return Result.error();
    }
}